﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QldtSdh.DAL.Models;

namespace QldtSdh.BLL
{
    public partial class BizDotTuyenSinh
    {
        /// <summary>
        /// Lấy danh sách
        /// </summary>
        /// <returns></returns>
        public static List<DotTuyenSinh> GetList()
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.DotTuyenSinhDbSet.OrderByDescending(e => e.NamTuyenSinh).ThenByDescending(e => e.DotTuyenSinh1).ToList();
            }
        }

        public static DotTuyenSinh GetById(string maDotTuyenSinh)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.DotTuyenSinhDbSet.Where(e => e.MaDotTuyenSinhGUID == maDotTuyenSinh).FirstOrDefault();
            }
        }

        public static CODE_RESULT_RETURN Add(DotTuyenSinh obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _ph = db.DotTuyenSinhDbSet.Where(e => e.NamTuyenSinh == obj.NamTuyenSinh && e.DotTuyenSinh1 == obj.DotTuyenSinh1).FirstOrDefault();
                if (_ph != null)
                    return CODE_RESULT_RETURN.MaTrung;
                obj.MaDotTuyenSinhGUID = Guid.NewGuid().ToString();
                db.DotTuyenSinhDbSet.Add(obj);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<DotTuyenSinh>(nhatKy, null, obj.MaDotTuyenSinhGUID, LOAIDOITUONG.ĐỢT_TUYỂN_SINH, LOAITHAOTAC.Thêm);
                BizNhatKyHoatDong.Add<DotTuyenSinh>(nhatKy, obj);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Update(DotTuyenSinh obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _ph = db.DotTuyenSinhDbSet.Where(e => e.NamTuyenSinh == obj.NamTuyenSinh && e.DotTuyenSinh1 == obj.DotTuyenSinh1 && e.MaDotTuyenSinhGUID != obj.MaDotTuyenSinhGUID).FirstOrDefault();
                if (_ph != null)
                    return CODE_RESULT_RETURN.MaTrung;
                _ph = db.DotTuyenSinhDbSet.Where(e => e.MaDotTuyenSinhGUID == obj.MaDotTuyenSinhGUID).FirstOrDefault();
                if (_ph == null)
                    return CODE_RESULT_RETURN.ThatBai;
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<DotTuyenSinh>(nhatKy, _ph, obj.MaDotTuyenSinhGUID, LOAIDOITUONG.ĐỢT_TUYỂN_SINH, LOAITHAOTAC.Chỉnh_sửa);
                
                _ph.NamTuyenSinh = obj.NamTuyenSinh;
                _ph.DotTuyenSinh1 = obj.DotTuyenSinh1;
                db.SaveChanges();
                BizNhatKyHoatDong.Add<DotTuyenSinh>(nhatKy, _ph);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Delete(DotTuyenSinh obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _ph = db.DotTuyenSinhDbSet.Where(e => e.MaDotTuyenSinhGUID == obj.MaDotTuyenSinhGUID).FirstOrDefault();
                if (_ph == null)
                    return CODE_RESULT_RETURN.ThatBai;
                var hvs = db.HocVienDbSet.Where(e => e.MaDotTuyenSinhGUID == obj.MaDotTuyenSinhGUID).Count();
                if (hvs != 0)
                    return CODE_RESULT_RETURN.ThatBai;
                db.DotTuyenSinhDbSet.Remove(_ph);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<DotTuyenSinh>(nhatKy, _ph, obj.MaDotTuyenSinhGUID, LOAIDOITUONG.ĐỢT_TUYỂN_SINH, LOAITHAOTAC.Xóa);
                BizNhatKyHoatDong.Add<DotTuyenSinh>(nhatKy, null);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }
    }

}
